package com.itheima_01.studentsystem;

import java.util.*;

public class StudentTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        lo:
        while (true) {
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("1、添加学生");
            System.out.println("2、删除学生");
            System.out.println("3、修改学生");
            System.out.println("4、查看学生");
            System.out.println("5、退出");
            String str = sc.next(); // 接收用户选择
            switch (str) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    //System.out.println("查看学生");
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
                default:
                    System.out.println("输入有误");
            }
        }

    }

    //添加学生信息-代码实现
    public static void addStudent(ArrayList<Student> stu_list) {
        Scanner sc = new Scanner(System.in);
        //解决添加学生学号的重复问题
        String sid;
        while (true) {
            System.out.println("请输入学生id：");
            sid = sc.next();
            int index = getIndex(stu_list, sid);
            if (index == -1) {
                break;
            } else {
                System.out.println("输入的学生id已存在");
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);
        stu_list.add(stu);
        System.out.println("添加成功");
    }

    //查询学生-代码实现
    public static void queryStudent(ArrayList<Student> stu_list) {
        if (stu_list.size() == 0) {
            System.out.println("学生列表信息为空");
            return;
        }
        System.out.println("学号\t 姓名\t 年龄\t 生日 ");
        for (int i = 0; i < stu_list.size(); i++) {
            Student item = stu_list.get(i);
            System.out.println(item.getSid() + "\t" + item.getName() + "\t" + item.getAge() + "\t" +
                    item.getBirthday());
        }
    }

    //判断学生学号是否存在-代码实现
    public static int getIndex(ArrayList<Student> stu_list, String sid) {
        int index = -1;
        for (int i = 0; i < stu_list.size(); i++) {
            Student stu = stu_list.get(i);
            String id = stu.getSid();
            if (id.equals(sid)) {
                index = i;
            }
        }

        return index;
    }

    //删除学生-代码实现
    public static void deleteStudent(ArrayList<Student> stu_list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生学号：");
        String stu_id = sc.next();
        int index = getIndex(stu_list, stu_id);
        if (index == -1) {
            System.out.println("学生学号不存在");
            return;
        } else {
            stu_list.remove(index);
            System.out.println("删除成功！");
        }
        return;
    }

    //修改学生-代码实现
    public static void updateStudent(ArrayList<Student> stu_list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生学号：");
        String stu_id = sc.next();
        int index = getIndex(stu_list, stu_id);
        if (index == -1) {
            System.out.println("学生学号不存在");
        } else {
            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生生日：");
            String birthday = sc.next();
            Student stu = new Student(stu_id, name, age, birthday);
            stu_list.set(index, stu);
            System.out.println("修改成功！");
        }
    }
}
